(function ($, global) {
  var defaults = {
    debug: false,
    jsApiList: [
      "updateAppMessageShareData",
      "updateTimelineShareData",
      "onMenuShareTimeline",
      "onMenuShareAppMessage",
      "onMenuShareQQ",
      "onMenuShareWeibo",
      "onMenuShareQZone",
      "startRecord",
      "stopRecord",
      "onVoiceRecordEnd",
      "playVoice",
      "pauseVoice",
      "stopVoice",
      "onVoicePlayEnd",
      "uploadVoice",
      "downloadVoice",
      "chooseImage",
      "previewImage",
      "uploadImage",
      "downloadImage",
      "translateVoice",
      "getNetworkType",
      "openLocation",
      "getLocation",
      "hideOptionMenu",
      "showOptionMenu",
      "hideMenuItems",
      "showMenuItems",
      "hideAllNonBaseMenuItem",
      "showAllNonBaseMenuItem",
      "closeWindow",
      "scanQRCode",
      "chooseWXPay",
      "openProductSpecificView",
      "addCard",
      "chooseCard",
      "openCard"
    ]
  };

  var WxConfig = function () {

  };

  WxConfig.prototype = {
    init: function (options) {
      options = $.extend({}, defaults, options);
      $.ajax({
        type: 'POST',
        // url: '/weixin/createShareConfig',
        url: _api.wxConfig,
        data: {
          url: options.url
        },
        dataType: 'json',
        success: function (resp) {
          if (resp.ret === 200) {
            const data = resp.data || {};
            wx.config({
              debug: options.debug,
              appId: data.appid,
              timestamp: data.timestamp,
              nonceStr: data.noncestr,
              signature: data.signature,
              jsApiList: options.jsApiList
            });

            // “分享给朋友”及“分享到QQ”
            wx.ready(function () {
              wx.updateAppMessageShareData({
                title: options.title,
                desc: options.desc,  // 分享描述
                link: options.link,
                imgUrl: options.imgUrl,
                success: function (res) {  // 设置成功
                },
                cancel: function (res) {
                },
                fail: function (result) {
                }
              });

              // “分享到朋友圈”及“分享到QQ空间”  无描述
              wx.updateTimelineShareData({
                title: options.title, // 分享标题
                link: options.link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: options.imgUrl, // 分享图标
                success: function () {
                  // 设置成功
                }
              });

            });

            wx.error(function (res) {
              // alert("wx_error:" + JSON.stringify(res));
            })
          } else {
            alert("wx_config_error: " + resp.msg);
          }
        }

      });
    }
  }

  /* CommonJS */
  if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
    module.exports = WxConfig;
  /* AMD */
  else if (typeof define === 'function' && define['amd'])
    define(function () {
      return WxConfig;
    });
  /* Global */
  else
    global['WxConfig'] = global['WxConfig'] || WxConfig;
})(jQuery, window);